package util

import (
	"fmt"
	rotatelogs "github.com/lestrrat-go/file-rotatelogs"
	"github.com/sirupsen/logrus"
	"strings"
	"time"
)

var (
	LogRus *logrus.Logger
)
func InitLog(configFile string) {
	viper:= CreateConfig(configFile)
	LogRus = logrus.New()
	switch strings.ToLower(viper.GetString("level")) {
	case "debug":
		LogRus.SetLevel(logrus.DebugLevel)
	case "info":
		LogRus.SetLevel(logrus.InfoLevel)
	case "warn":
		LogRus.SetLevel(logrus.WarnLevel)
	case "error":
		LogRus.SetLevel(logrus.ErrorLevel)
	case "panic":
		LogRus.SetLevel(logrus.PanicLevel)
	default:
		panic(fmt.Errorf("invalid log level: %s", viper.GetString("level")))
	}
	LogRus.SetFormatter(&logrus.TextFormatter{
		TimestampFormat: "2006-01-02 15:04:05",
	})
	logFile:= ProjectRootPath +viper.GetString("file")
	fout, err:=rotatelogs.New(
		logFile+"_%Y%m%d%H",
		//rotatelogs.WithLinkName(logFile),		// 设置软连接
		rotatelogs.WithRotationTime(3*time.Hour),
		rotatelogs.WithMaxAge(time.Hour*24*3),
		)
	if err!=nil {
		panic(err)
	}
	LogRus.SetOutput(fout)       //设置日志文件
	LogRus.SetReportCaller(true) //输出是从哪里调的日志打印
}